class GroupaliasController {
    constructor($cookieStore, $state, dialogService, GroupaliasService){

    	const vm = this;

		vm.$cookieStore      = $cookieStore;
		vm.$state            = $state;
		vm.dialogService     = dialogService;
		vm.GroupaliasService = GroupaliasService;

        // 表单数据初始化
        vm.groupalias = {
            alias: ''
        }

    }

    // 表单提交
    submitForm(isValid) {

        const vm = this;

        // 验证失败, 提示用户
        !isValid && !vm.groupalias.alias && $.blink(angular.element(document.getElementsByName('alias')), 5, 100)

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            let params = {
                alias: vm.groupalias.alias,
                group_name: vm.$state.params.group_name
            }

            console.log(params)

            // 新增分组
            vm.GroupaliasService.postGroupaliasCreate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
            .then( data => {

                // 成功调用
                data.code==0&&vm.dialogService.success('创建成功', 'dashboard.race.groupaliaslist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                
                console.log(data)
            }, error => console.log('error'));
        }

        return false;
    }
}

GroupaliasController.$inject = ['$cookieStore', '$state', 'dialogService', 'GroupaliasService']; 

export default GroupaliasController;